package com.qdsgjsfk.vision.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.MutableLiveData;
import android.support.annotation.NonNull;

import com.rest.business.RestProxy;
import com.rest.response.CollectClassResponse;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import com.qdsgjsfk.vision.util.NetUtil;

/**
 * Created by HP on 2019/11/27.
 */

public class CollectClassViewModel extends AndroidViewModel {


    private final MutableLiveData<List<CollectClassResponse.Class>> mItems = new MutableLiveData<>();

    public CollectClassViewModel(@NonNull Application application) {
        super(application);
    }

    public MutableLiveData<String> getmFaceDesc() {
        return mFaceDesc;
    }

    private final MutableLiveData<String> mFaceDesc = new MutableLiveData<>();

    public MutableLiveData<List<CollectClassResponse.Class>> getmItems() {
        return mItems;
    }


    public void getCollectClass(String isFinish, String studentName,String taskId) {
        RestProxy.getInstance().getCollectClass(isFinish, null,taskId, new Observer<CollectClassResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(CollectClassResponse collectClassResponse) {
                mItems.setValue(reRangeList(collectClassResponse.data.pageInfo.list));
                mFaceDesc.setValue(collectClassResponse.data.faceDesc);
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                NetUtil.onError(e,getApplication());
            }

            @Override
            public void onComplete() {

            }
        });
    }


    private List<CollectClassResponse.Class> reRangeList(List<CollectClassResponse.Grade> list) {
        List<CollectClassResponse.Class> newList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            CollectClassResponse.Class tempClass = new CollectClassResponse.Class();
            tempClass.prefxName = list.get(i).prefxName;
            tempClass.type = 0;
            newList.add(tempClass);
            for (int j = 0; j < list.get(i).classList.size(); j++) {
                list.get(i).classList.get(j).type = 1;
                newList.add(list.get(i).classList.get(j));
            }
        }
        return newList;
    }
}
